package com.demo.abmi;

import java.text.DecimalFormat;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Abmi extends Activity {

	private static final String TAG = "aBmi";
	
	protected static final int MENU_ABOUT = Menu.FIRST;
	
	private static final String PREF = "BMI_PREF";
	private static final String PREF_FEET = "BMI_FEET";
	private static final String PREF_INCH = "BMI_INCH";
	/**
	static final String[] feets = new String[]{
		"2 Feet",
		"3 Feet",
		"4 Feet",
		"5 Feet",
		"6 Feet",
		"7 Feet",
		"8 Feet"
	};**/

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/**
		ArrayAdapter<String> adapter_feet = new ArrayAdapter<String>(Abmi.this, android.R.layout.simple_spinner_item, feets);
		adapter_feet.setDropDownViewResource(android.R.layout.simple_spinner_item);
		
		Resources res = getResources();//获得资源对象
		Configuration conf = res.getConfiguration();
		conf.locale = Locale.SIMPLIFIED_CHINESE;
		DisplayMetrics dm = res.getDisplayMetrics();//更新到屏幕显示
		res.updateConfiguration(conf, dm);
		**/
		setContentView(R.layout.activity_abmi);
		findViews();
		setListeners();
		restorePrefs();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		SharedPreferences settings = getSharedPreferences(PREF,0);
		settings.edit()
		    .putInt(PREF_FEET, field_feet.getSelectedItemPosition())
		    .putInt(PREF_INCH, field_inch.getSelectedItemPosition())
		    .commit();
	}
	

	private void restorePrefs() {
		// TODO Auto-generated method stub
		SharedPreferences settings = getSharedPreferences(PREF, 0);
		Integer pref_feet = settings.getInt(PREF_FEET, 0);
		field_feet.setSelection(pref_feet);
		field_inch.requestFocus();
		
		Integer pref_inch = settings.getInt(PREF_INCH, 0);
		field_inch.setSelection(pref_inch);
		field_weight.requestFocus();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		super.onCreateOptionsMenu(menu);
		Log.d(this.toString(), "open memu");
		menu.add(0, MENU_ABOUT, 0, R.string.menu_about);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
		super.onOptionsItemSelected(item);
		Log.v(this.toString(), "select menu item");
		switch(item.getItemId()){
		case MENU_ABOUT:
			openOptionsDialog();
			break;
		}
		return true;
	}
	
	private void openOptionsDialog() {
		// TODO Auto-generated method stub
		Log.d(this.toString(), "open dialog");
		/*
		Resources res = getResources();//获得资源对象
		Configuration conf = res.getConfiguration();
		if (conf.locale == Locale.SIMPLIFIED_CHINESE){
			System.out.print("SIMPLIFIED_CHINESE");
		}
		conf.locale = Locale.SIMPLIFIED_CHINESE;
		DisplayMetrics dm = res.getDisplayMetrics();//更新到屏幕显示
		res.updateConfiguration(conf, dm);
		*/
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(Abmi.this);
		dialog.setTitle(R.string.about_title);
		dialog.setMessage(R.string.about_msg);
		dialog.setPositiveButton(R.string.ok_label,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub

					}
				});
		dialog.show();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(this)
					.setMessage("确定退出？")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									finish();
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub

								}
							}).show();
		}
		return super.onKeyDown(keyCode, event);
	}

	private Button btn_submit;
	// private EditText field_feet;
	// private EditText field_inch;
	// private EditText field_weight;
	private Spinner field_feet;
	private Spinner field_inch;
	private EditText field_weight;
	private TextView view_result;
	private TextView view_suggest;

	private void findViews() {
		Log.d(TAG, "find views");

		field_feet = (Spinner) findViewById(R.id.feet);
		field_inch = (Spinner) findViewById(R.id.inch);
		field_weight = (EditText) findViewById(R.id.weight);
		btn_submit = (Button) findViewById(R.id.btn_submit);
		/**
		 * field_feet = (EditText) findViewById(R.id.feet); field_inch =
		 * (EditText) findViewById(R.id.inch); field_weight = (EditText)
		 * findViewById(R.id.weight);
		 **/
		view_result = (TextView) findViewById(R.id.result);
		view_suggest = (TextView) findViewById(R.id.suggest);

		ArrayAdapter<CharSequence> adapter_feet = ArrayAdapter
				.createFromResource(this, R.array.feets,
						android.R.layout.simple_spinner_dropdown_item);
		adapter_feet
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		ArrayAdapter<CharSequence> adapter_inch = ArrayAdapter
				.createFromResource(this, R.array.inches,
						android.R.layout.simple_spinner_dropdown_item);
		adapter_inch
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		field_feet.setAdapter(adapter_feet);
		field_inch.setAdapter(adapter_inch);
	}

	private void setListeners() {
		Log.d(TAG, "set listener");

		field_feet.setOnItemSelectedListener(getFeet);
		field_inch.setOnItemSelectedListener(getInch);
		btn_submit.setOnClickListener(calcUsBMI);
	}

	private int feet;
	private int inch;

	//使用匿名类来实现接口
	private Spinner.OnItemSelectedListener getFeet = new Spinner.OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			// TODO Auto-generated method stub
			feet = parent.getSelectedItemPosition() + 2;// 获取索引值并加2得到实际值

		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			// TODO Auto-generated method stub

		}

	};

	
	//使用匿名类来实现接口
	private Spinner.OnItemSelectedListener getInch = new Spinner.OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			// TODO Auto-generated method stub
			inch = parent.getSelectedItemPosition() + 1;

		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			// TODO Auto-generated method stub

		}

	};

	private Button.OnClickListener calcUsBMI = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			DecimalFormat df = new DecimalFormat("0.00");
			try {
				/*
				 * double height =
				 * (Double.parseDouble(field_feet.getText().toString())*12+
				 * Double
				 * .parseDouble(field_inch.getText().toString()))*2.54/100;//
				 * (feet*12+inch)*0.0254
				 */
				double weight = Double.parseDouble(field_weight.getText()
						.toString()) * 0.45359;

				double height = (feet * 12 + inch) * 0.0254;
				double bmi = weight / (height * height);

				view_result.setText(getText(R.string.bmi_result)
						+ df.format(bmi));

				if (bmi > 27) {
					view_suggest.setText(R.string.advice_fat);
				} else if (bmi > 25) {
					view_suggest.setText(R.string.advice_heavy);
				} else if (bmi < 20) {
					view_suggest.setText(R.string.advice_light);
				} else {
					view_suggest.setText(R.string.advice_average);
				}
			} catch (Exception obj) {
				Toast.makeText(Abmi.this, R.string.input_error,
						Toast.LENGTH_LONG).show();
			}
		}

	};

}
